package com.passion.auth.config;

import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.dao.SaTokenDaoRedisJackson;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * 类描述：sa-token拦截器配置
 *
 * @author ZhuYin
 * @since 2024年12月25日
 */
@Configuration
public class SaTokenConfig implements WebMvcConfigurer {

    @Bean
    @ConfigurationProperties(prefix = "auth.exclude")
    public ExcludePaths excludePaths() {
        return new ExcludePaths();
    }

    @Bean
    public SaTokenDao saTokenDao() {
        // 返回Redis实现
        return new SaTokenDaoRedisJackson();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> paths = excludePaths().paths;
        // 注册Sa-Token的Session拦截器，打开注解功能
        registry.addInterceptor(new SaInterceptor( x -> StpUtil.checkLogin()))
                .excludePathPatterns(paths)
                .addPathPatterns("/**");
    }

    @Setter
    @Getter
    public static class ExcludePaths {
        private List<String> paths;
    }
}